<template>
    <div>
        <div class="table_box">
            <tyTable :searchFlag="true"
                     :listApi="$api.listUpstreamServerInfo"
                     :deleteApi="$api.deleteUser"
                     :delete-flag="false"
                     :columnWidth="180"
                     :fixParamFunc="fixParamFunc"
                     :openDrawer="openDrawer" ref="tyTable">
                <template v-slot:search="{ param}">
                    <el-form-item label="upstream:" prop="name">
                        <el-select v-model="param.upstreamInfoId" placeholder="请选择" clearable>
                            <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>

                </template>

                <!--            按钮-->
                <template v-slot:search-operation>
                    <el-button type="primary" @click="$router.go(-1)" icon="el-icon-back">返回</el-button>
                </template>

                <template v-slot:table>
                    <el-table-column type="index" label="序号" align="center" width="52"></el-table-column>
                    <el-table-column prop="upstreamInfoName" label="upstream" align="center"></el-table-column>
                    <el-table-column prop="address" label="地址" align="center"></el-table-column>
                    <el-table-column prop="text" label="内容" align="center"></el-table-column>
                    <el-table-column prop="sort" label="是否启用" align="center"  >
                        <template slot-scope="scope">
                            <div v-if="scope.row.isEnable == 1" style="color: #67C23A">启用</div>
                            <div v-else style="color: #909399">禁用</div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="sort" label="启用操作" align="center"  >
                        <template slot-scope="scope">
                            <el-switch
                                v-model="scope.row.isEnable"
                                active-color="#13ce66"
                                :active-value="1"
                                :inactive-value="0"
                                @change="changeStatus(scope.row)"
                            >
                            </el-switch>
                        </template>
                    </el-table-column>
                </template>

                <template v-slot:extra="{row}">

                </template>
            </tyTable>
        </div>

        <tyAddUpdate :model="formData"
                     :add-api="$api.addUser"
                     :update-api="$api.updateUpstreamServerInfo"
                     :dialogSize="'60%'"
                     ref="tyAddUpdate"
        >
            <el-form label-position="left" label-width="100px" :model="formData" :rules="rules">
                <el-form-item label="upstream:" prop="upstreamInfoName">
                    <el-input v-model="formData.upstreamInfoName" placeholder=""  clearable disabled
                              maxlength="300"></el-input>
                </el-form-item>
                <el-form-item label="地址:" prop="address">
                    <el-input v-model="formData.address" placeholder=""  clearable
                              maxlength="300"></el-input>
                </el-form-item>
                <el-form-item label="内容:" prop="text">
                    <el-input v-model="formData.text" placeholder=""  clearable
                              maxlength="300"></el-input>
                </el-form-item>
                <el-form-item label="是否启用:" prop="isEnable">
                    <el-switch
                        v-model="formData.isEnable"
                        active-color="#13ce66"
                        :active-value="1"
                        :inactive-value="0"
                    >
                    </el-switch>

                </el-form-item>

            </el-form>
        </tyAddUpdate>

    </div>
</template>

<script>
import {tyAddUpdateMixins, tyConfirmMixins} from "@/mixins/addEditFormMixins";

export default {
    name: "upstreamServerConfig",
    mixins: [tyAddUpdateMixins, tyConfirmMixins],
    data(){
        return{
            formData : {},
            loading:false,
            options: [],

            rules: {
                address: [
                    {required: true, message: "地址不能为空", trigger: "blur"}
                ],
            }
        }
    },
    methods : {
        getSelect(){
            this.$api.listUpstreamInfo({page : 1 , limit : 1000}).then((res) => {
                this.options = []
                let array = res.records
                for(let item of array){
                    this.options.push({value : item.id, label : item.name})
                }
            })
        },
        fixParamFunc(form){
            let confInfoId = this.$route.query.confInfoId
            if(confInfoId !=  null){
                form.confInfoId = confInfoId
            }
        },
        changeStatus(row){
            this.$api.updateUpstreamServerStatus(row).then((res) => {
                this.$refs.tyTable.getList()
                this.$message({
                    message: '调用成功',
                    type: 'success'
                });
            })
        }
    },
    mounted() {
        this.getSelect()
    },

}

</script>
<style scoped lang="scss">
    .limit-height{
        height:50px;
    }
</style>